package com.thoughtworks;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseUtil {
    public static final String url = "jdbc:mysql://localhost:3306/javaJdbc?useUnicode=true&characterEncoding=utf-8&serverTimezone=Hongkong";
    public static final String user = "root";
    public static final String password = "19950603abc";
    private static Connection conn;

    static{
        try {
            //1.加载驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 获得数据库连接
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        try{
            return DriverManager.getConnection(url, user, password);
        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException("Can not connect to DataBase.", e);
        }
    }
}
